package com.example.admin.myapplication.util;

import android.text.TextUtils;

import com.example.admin.myapplication.db.City;
import com.example.admin.myapplication.db.Country;
import com.example.admin.myapplication.db.Province;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Admin on 2017/5/27.
 */

public class Utility {
    /**
     * 处理省级数据
     */
    public static boolean handleProvinceRequest(String response){
        if(!TextUtils.isEmpty(response)){
            try{
                JSONArray allprovince = new JSONArray(response);
                for(int i = 0 ; i <allprovince.length();i++){
                    JSONObject provinceObject = allprovince.getJSONObject(i);
                    Province province = new Province();
                    province.setProvinceName(provinceObject.getString("name"));
                    province.setProvinceCode(provinceObject.getInt("id"));
                    province.save();
                }
                return true;
            }
            catch (JSONException e){
                e.printStackTrace();
            }
        }
        return  false;
    }
    /**
     * 处理省级数据
     */
    public static boolean handleCityRequest(String response,int provinceid){
        if(!TextUtils.isEmpty(response)){
            try{
                JSONArray allCities = new JSONArray(response);
                for(int i = 0 ; i <allCities.length();i++){
                    JSONObject cityObject = allCities.getJSONObject(i);
                    City city = new City();
                    city.setCityName(cityObject.getString("name"));
                    city.setCityCode(cityObject.getInt("id"));
                    city.setProvinceId(provinceid);
                    city.save();
                }
                return true;
            }
            catch (JSONException e){
                e.printStackTrace();
            }
        }
        return  false;
    }
    /**
     * 处理县市级数据
     */
    public static boolean handleCountryRequest(String response,int cityid){
        if(!TextUtils.isEmpty(response)){
            try{
                JSONArray allCountry = new JSONArray(response);
                for(int i = 0 ; i <allCountry.length();i++){
                    JSONObject countryObject = allCountry.getJSONObject(i);
                    Country country = new Country();
                    country.setCountryName(countryObject.getString("name"));
                    country.setWeatherId(countryObject.getInt("weather_id"));
                    country.save();
                }
                return true;
            }
            catch (JSONException e){
                e.printStackTrace();
            }
        }
        return  false;
    }


}
